
import pandas as pd
import numpy as np
from pyecharts import Bar
import sqlite3

# Create your ana here.


if __name__=='__main__':

    db_name='../db.sqlite3'

else:

    db_name = 'db.sqlite3' 


class UserAan:

    def __init__(self):

        #self.db_name='../db.sqlite3'

        self.conn=sqlite3.connect(db_name)

        self.df=pd.read_sql('SELECT * FROM users_csv',self.conn)

        print(self.df.head())

  
    #生成series

    def series_to_bar(self,series, name='test', table_name='test1'):

        bar = Bar(name)

        axis0=series.index

        axis1=np.round(series.values.ravel(),1)

        bar.add("商家A", axis0, axis1, is_label_show=True, is_datazoom_show=True)

        return bar


    def how_many_are_sold_per_item(self):

        df1 = self.df.groupby('item_name').sum()

        df1.drop('order_id', axis=1,inplace=True)

        return df1

    def bar_how_many_are_sold_per_item(self):

        info=self.how_many_are_sold_per_item()

        bar=self.series_to_bar(info)

        return bar

    #每一种商品的消费额

    def how_much_are_sold_per_item(self):

        ipr=self.df.quantity*(self.df.item_price.str.replace("$","").astype("float64"))

        df1=self.df.copy()

        df1['ipr'] = ipr

        df1_group=df1.groupby("item_name").sum()

        df1_group.drop("order_id",axis=1,inplace=True)

        df1_group.drop("quantity", axis=1, inplace=True)

        return df1_group

    def bar_how_much_are_sold_per_item(self):

        info=self.how_much_are_sold_per_item()

        bar=self.series_to_bar(info)

        return bar

    

UserAan_obj=UserAan()

#if __name__=='__main__':


    #x=UserAan()

    



    
